package com.cw.subject.domain.handler.subject;

import com.cw.subject.domain.convert.MultipleSubjectConverter;
import com.cw.subject.domain.entity.SubjectAnswerBO;
import com.cw.subject.domain.entity.SubjectOptionBO;
import com.cw.subject.infra.basic.entity.SubjectMultiplePO;
import com.cw.subject.infra.basic.service.SubjectRadioPOService;
import com.cw.subject.common.enums.IsDeletedFlagEnum;
import com.cw.subject.common.enums.SubjectInfoTypeEnum;
import com.cw.subject.domain.convert.RadioSubjectConverter;
import com.cw.subject.domain.entity.SubjectInfoBO;
import com.cw.subject.infra.basic.entity.SubjectRadioPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 单选题目的策略类
 *
 * @author: 江传文
 * @date: 2024/4/12 16:50
 */
@Component
public class RadioTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectRadioPOService subjectRadioPOService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO; // 只处理RADIO类型的
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //单选题目的插入
        List<SubjectRadioPO> subjectRadioList = new LinkedList<>();

        /*subjectInfoBO.getOptionList(): 获取subjectInfoBO对象中的optionList属性,该属性通常是一个列表(或集合)
          .forEach(option -> { });: 这是一个Lambda表达式,用于对列表中的每个元素执行特定的操作.在这种情况下,
           option -> { }表示对列表中的每个元素执行一个操作.在 { } 内,您可以编写要执行的操作代码。*/
        if (subjectInfoBO != null && subjectInfoBO.getOptionList() != null) {
            subjectInfoBO.getOptionList().forEach(option -> {
                    SubjectRadioPO subjectRadioPO = RadioSubjectConverter.INSTANCE.convertBoToEntity(option);
                    subjectRadioPO.setSubjectId(subjectInfoBO.getId());
                    subjectRadioPO.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
                    subjectRadioList.add(subjectRadioPO);
            });
            subjectRadioPOService.batchInsert(subjectRadioList);
        }
    }

    @Override
    public SubjectOptionBO query(int subjectId) {
        SubjectRadioPO subjectRadioPO = new SubjectRadioPO();
        subjectRadioPO.setSubjectId(subjectId);
        List<SubjectRadioPO> result = subjectRadioPOService.queryByCondition(subjectRadioPO);
        List<SubjectAnswerBO> subjectAnswerBOList = RadioSubjectConverter.INSTANCE.convertEntityToBoList(result);

        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }
}
